from django.utils.deprecation import MiddlewareMixin
from django.shortcuts import redirect

class AuthMidleware(MiddlewareMixin):

    def process_request(self, request):
        # 1.排除那些不需要登录验证就能访问的页面
        # request.path_info 获取当前用户请求的URL
        if request.path_info in ["/login/","/image/code/"]:
            return
        # 2.读取当前访问的用户的session信息，如果能被读到，则说明已经登录过，继续往前走
        info_dict = request.session.get("info")
        print(info_dict)
        if info_dict:
            return

        # 3.没有登录过，重新回到登录界面
        return redirect("/login/")